<?php

class Category_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Registry::get('layout')->setLayout('node');
        
        $activeResource=Category_Model_Category::RESOURCE_NAME;
        
        // seting active resource
        Zend_Registry::set('resource',$activeResource);
        
        $this->view->headTitle()->append(Zend_Registry::get('locale')->_('Category'));

        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->flash_messages = $this->_flashMessenger->getMessages();
    }

    public function indexAction()
    {
        $category=(string) $this->_getParam('category');
        $key=(int) $this->_getParam('key');
        
        if (!Zend_Registry::get('user')->canView() || empty($category))
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }
        
        $this->view->headTitle()->append(Application_Model_NodeCategories::getCategory($category));
        
        $cache=Zend_Registry::get('cache');
        
        if(!$result = $cache->load('category_'.md5($category).'_'.$key)) 
        {
            $nodeTbl=new Application_Model_NodeDb();
            
            $result=$nodeTbl->getNodesPageByClause(array('category=?'=>$category,
                                                                         'type=?'=>'node',
                                                                         'published=?'=>'1'
                                                                         ),
                                                                   $key,5);
                                                                   
            $cache->save($result, 'category_'.md5($category).'_'.$key);
        }
        
        list($nodes,$paginator)=$result;
        
        $this->view->nodes=$nodes;
        $this->view->paginator=$paginator;
    }


}

